package com.baizhi.cmfzdemo.controller;

import com.baizhi.cmfzdemo.entity.CmfzCounter;
import com.baizhi.cmfzdemo.entity.CmfzGuru;
import com.baizhi.cmfzdemo.service.CmfzCounterService;
import com.baizhi.cmfzdemo.service.CmfzGuruService;
import com.baizhi.cmfzdemo.util.ClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * Created by 54433 on 2019/4/4.
 */
@Controller
@RequestMapping("/cmfzGuru")
public class CmfzGuruController {
    @Autowired
    private CmfzGuruService cmfzGuruService;

    //展示所有
    @RequestMapping("/queryAllCmfzGuru")
    @ResponseBody
    public Map queryAllCmfzGuru(int page,int rows){
        System.out.println(cmfzGuruService.queryAllCmfzGuru(page,rows));
        return cmfzGuruService.queryAllCmfzGuru(page,rows);
    }
    @RequestMapping("/deleteCmfzGuru")
    @ResponseBody
    public Map deleteCmfzGuru(int [] ids){
        return cmfzGuruService.deleteCmfzGuru(ids);
    }
    @RequestMapping("/insertCmfzGuru")
    @ResponseBody
    public Map insertCmfzGuru(CmfzGuru cmfzGuru,MultipartFile guruimg, HttpServletRequest request) throws IOException {
//获取到原始文件名；上传上来的那个文件的名字
        System.out.println(guruimg);
        String fileName = guruimg.getOriginalFilename();

        cmfzGuru.setGuruImage("/img/"+fileName);
/*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        guruimg.transferTo(new File(realPathNewMethod+"/"+fileName));
        return cmfzGuruService.addCmfzGuru(cmfzGuru);
    }
    @RequestMapping("/updateCmfzGuru")
    @ResponseBody
    public Map updateCmfzGuru(CmfzGuru cmfzGuru, MultipartFile gpic, HttpServletRequest request) throws IOException {
        String fileName = gpic.getOriginalFilename();

        cmfzGuru.setGuruImage("/img/"+fileName);
        /*获取到upload的绝对路径*/
        String realPathNewMethod = request.getServletContext().getRealPath("img");
        gpic.transferTo(new File(realPathNewMethod+"/"+fileName));
        return cmfzGuruService.updateCmfzGuru(cmfzGuru);
    }
    @RequestMapping("/downLoad")
    @ResponseBody
    public String downLoad() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
        List<CmfzGuru> cmfzGurus = cmfzGuruService.queryAll();
        int i = (int) (Math.random()*100);
        String gurus = "guru"+i;
        ClassUtil.setPoi(cmfzGurus,gurus);
        return "success";
    }

    public void load(HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = "hhhhh.xls";
        response.setCharacterEncoding("UTF-8");
        //response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/octet-stream;charset=UTF-8");
        fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");// 为了解决中文名称乱 码问题
        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
    }

    /**
     * 查询所有的上师
     *
     */
    @RequestMapping("/showAllGuru")
    @ResponseBody
    public List<CmfzGuru> showAllGuru(){
        return  cmfzGuruService.showAllGuru();
    }

}
